package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Actor;
import org.akomantoso.core.data.attribute.Date;
import org.akomantoso.core.data.attribute.Outcome;
import org.akomantoso.core.data.attribute.Role;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;

/**
 * The element step is a metadata element specifying facts about a workflow step occurred to the document. For each
 * event, a date, a type, an actor (and the corresponding role) that generated the action must be referenced. The
 * outcome, too, can be specified.
 * 
 * <pre>
 * 	<xsd:element name="step">
 * 		<xsd:complexType>
 * 			<xsd:complexContent>
 * 				<xsd:extension base="anyOtherType">
 * 					<xsd:attributeGroup ref="date"/>
 * 					<xsd:attributeGroup ref="actor"/>
 * 					<xsd:attributeGroup ref="role"/>
 * 					<xsd:attributeGroup ref="outcome"/>
 * 				</xsd:extension>
 * 			</xsd:complexContent>
 *		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 */
public class Step extends AnyOtherType implements Role, Date, Outcome, Actor{

	public static final String ELEMENT = "step";

	private String _as;
	private Calendar _date;
	private String _outcome;
	private String _actor;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Date#getDate()
	 */
	@Override
	public Calendar getDate() {
		return _date;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#setDate(java.util.Calendar)
	 */
	@Override
	public void setDate(Calendar date) {
		_date = date;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Role#getAs()
	 */
	@Override
	public String getAs() {
		return _as;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Role#setAs(java.lang.String)
	 */
	@Override
	public void setAs(String as) {
		_as = as;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Outcome#getOutcome()
	 */
	@Override
	public String getOutcome() {
		return _outcome;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Outcome#setOutcome(java.lang.String)
	 */
	@Override
	public void setOutcome(String outcome) {
		_outcome = outcome;
	}
	
	/**
	 * @see org.akomantoso.core.data.attribute.Actor#setActor(java.lang.String)
	 */
	@Override
	public void setActor(String actor) {
		_actor = actor;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Actor#getActor()
	 */
	@Override
	public String getActor() {
		return _actor;
	}

	/**
	 * @see org.akomantoso.core.data.AnyOtherType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeDate(out, this);
		OutputUtil.writeOutcome(out, this);
		OutputUtil.writeActor(out, this);
		OutputUtil.writeRole(out, this);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.AnyOtherType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_date = DateUtil.convert(in.readAttribute(Date.ATTRIBUTE));
		_as = in.readAttribute(Role.ATTRIBUTE);
		_outcome = in.readAttribute(Outcome.ATTRIBUTE);
		_actor = in.readAttribute(Actor.ATTRIBUTE);
		super.readExternal(in);
	}
	
	
}
